package com.ccp.dev.qbdms.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.base.BaseController;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.model.WQbdQualityAnalysisReport;
import com.ccp.dev.qbdms.service.WQbdQualityAnalysisReportService;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(ZhiLiangConst.URL_API + ZhiLiangConst.URL_VERSION)
public class WQbdQualityAnalysisReportApiController extends BaseController {
    @Resource
    private WQbdQualityAnalysisReportService wQbdQualityAnalysisReportService;
    @Resource
    private ExtendSysOrgService extendSysOrgService;

    /**
     * 质量综合分析报告下发的接口
     *
     * @param jsonstr
     * @return
     */
    @PostMapping("/wQbdQualityAnalysisReport/reportSend")
    public Object reportSend(@RequestBody String jsonstr) {
        //解析json
        JSONObject jsonobj = JSONObject.parseObject(jsonstr);
        // 判断是否包含附件
        boolean hasFile = jsonobj.containsKey(ZhiLiangConst.API_REPORT_PARA_LIST_FILE);
        List<ExtendSysFile> filelist= Lists.newArrayList();
        if (hasFile) {
            JSONArray filelistjson = jsonobj.getJSONArray(ZhiLiangConst.API_REPORT_PARA_LIST_FILE);
            filelist = filelistjson.toJavaList(ExtendSysFile.class);
        }
        // 获取上报参数,是一个list
        JSONArray listobj = jsonobj.getJSONArray(ZhiLiangConst.API_REPORT_PARA_LIST);
        List<WQbdQualityAnalysisReport> list = listobj.toJavaList(WQbdQualityAnalysisReport.class);
        int result;
        try {
            result=wQbdQualityAnalysisReportService.saveSendReport(list,filelist);
        }catch (Exception e){
            e.printStackTrace();
            result=0;
        }
        if (result==list.size()) {
            return success("成功");
        }else{
            return fail("失败");
        }
    }

}
